<?php
/**
 * User: zj
 * Date: 2017/5/6
 * Time: 17:28
 * Progress:0%
 */

namespace logic\admin\shop;


use model\ShopGoods;
use model\Account;
use model\ShopGoodsCategory;
use model\ShopStore;
use think\Db;

class LShopStore
{
    //店铺列表
    public function index_data($param){
        $model = new ShopStore();
        $where=array();

        $keywords = $param['search'];
        if (!empty($keywords)) {
            $where['name'] = array('like', "%{$keywords}%"); //查询条件
        }

        $status = $param['status'];
        if ($status > -1) {
            $where['flag_lock'] = $status;
        }

        if(!empty($param['begindate'])){
            $begin_date = strtotime($param['begindate']);
        }else{
            $begin_date = '';
        }
        if(!empty($param['enddate'])){
            $end_date = strtotime('+1 day',strtotime($param['enddate']));
        }else{
            $end_date = '';
        }

        if (!empty($begin_date) && !empty($end_date)) {
            $where['add_time'] = array('between', "{$begin_date},{$end_date}");
        }else if(!empty($begin_date) && empty($end_date)){
            $where['add_time'] = array('egt', $begin_date);
        }else if(empty($begin_date) && !empty($end_date)){
            $where['add_time'] = array('elt', $end_date);
        }

        $total=$model->where($where)->count();
            //记录总数

        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['sort'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['add_time'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束

        $rows = $model
            ->where($where)
            ->limit($start, $page_size)
            ->order($sort)
            ->select();

        foreach ($rows as &$v){
            $v["bizhours"] = $v["bizhours_start"]."至".$v["bizhours_end"];

        }
        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );

        return $array;
    }

    //用户列表
    public function account_data(){
        $model = new Account();
        $where['flag_lock'] = 0;
        $data = $model->where($where)->field("id,nick,name")->select();
        return $data;
    }

    //店铺添加
    public function insert_data($param){

        $model = new ShopStore();
        $res=$model->validate(true)->save($param);

        //添加店铺的同时，默认给该店铺添加一个名称为'未分类'的分类
//        $store_id = $model->id;
//        $model_sgc =new ShopGoodsCategory();
//        $param2=array(
//            'name'=>'未分类',
//            'sort'=>0,
//            'flag_use'=>1,
//            'icon'=>'',
//            'store_id'=>$store_id,
//            'parent_id'=>0,
//            'isdefault'=>1,
//        );
//        $res2=$model_sgc->insert($param2);


        if($res!== false){
            return array(
                'success'=>true,
                'message'=>'添加成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }

    //编辑页面
    public function edit_data($id){
        $data= ShopStore::get(['id'=>$id]);
        return $data->getData();
    }

    //店铺信息更新
    public function  update_data($id,$param){
        $model = new ShopStore();
        $res = $model->validate(true)->save($param,['id'=>$id]);
        if($res){
            return array(
                'success'=>true,
                'message'=>'修改成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }

    //店铺删除
    public function delete_data($id){
        //先判断该店铺下是否存在商品，已经存在商品的店铺不允许删除
        $model_sg =new ShopGoods();
        $shop_goods_num =$model_sg->where(['store_id'=>$id])->count();

        if($shop_goods_num>0){
            return array(
                'success'=>false,
                'message'=>'该店铺有发布商品，不允许删除'
            );
        }

        //删除店铺
        $model = new ShopStore();
        $res1 = $model->where(['id'=>$id])->delete();
        //删除店铺下所有分类
        $model_sgc =new ShopGoodsCategory();
        $res2 =$model_sgc->where(['store_id'=>$id])->delete();

        if($res1!==false && $res2!==false){
            return array(
                'success'=>true,
                'message'=>'删除成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()

            );
        }

    }



}